#! /usr/bin/env python
#coding=utf-8

import os
import sys

if os.getuid() == 0:
	pass
else:
	print "当前用户不是root用户，请以root用户执行脚本"
	sys.exit(1)

version = raw_input("请输入您想安装的python版本（3.6），系统都自带2.7了，所以没有选择了！")

if version == "3.6":
	url = "https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz"
else:
	print "您输入的版本号有误，请输入3.6"
	sys.exit(1)

print "开始安装依赖关系"
cmd1 = "yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y"
res1 = os.system(cmd1)
if res1 != 0:
	print "安装依赖包失败，请确认系统是否为Centos，是否联网"
	sys.exit(1)

print "开始下载python安装包"
print "国内网络直接下载可能会很慢"
cmd2 = "wget " + url
res2 = os.system(cmd2)
if res2 != 0:
	print "下载源码包失败，请检查当前网络。"
	sys.exit(1)


package_name = "Python-3.6.6"

print "开始解压缩"
cmd3 = "tar xf " + package_name + ".tgz"
res3 = os.system(cmd3)
if res3 != 0:
	os.system("rm " + package_name + ".tgz")
	print "解压源码包失败，请重新运行脚本下载源码包"
	sys.exit(1)

print "开始编译安装"
cmd4 = "cd " + package_name + " && ./configure --prefix=/usr/local/python3 && make &&make install"
res4 = os.system(cmd4)
if res4 != 0:
	print "编译python源码失败，请检查是否缺少依赖库"
	sys.exit(1)

print "正在完善最后的设置"
cmd5 = "ln -s /usr/local/python3/bin/python3 /usr/bin/python3"
os.system(cmd5)

cmd6 = "ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3"
os.system(cmd6)

cmd7 = "pip3 install --upgrade pip"
os.system(cmd7)

print "安装完成,您可以使用python3和pip3 测试新安装的python"
print "python3的安装路径为/usr/local/python3/，与python2.7共存"